#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ia09.h"
int main(int argc, char * * argv)
{
  /* if argc is not 2, return EXIT_FAILURE */
  if (argc != 4)
    {
      printError(ARGC_ERROR);
      return EXIT_FAILURE;
    }
  int val2 = (int) strtol(argv[2], NULL, 10);
  int val3 = (int) strtol(argv[3], NULL, 10);

  if (strcmp(argv[1], "-b") == 0)
    {
      if ((val2 < 2) || (val2 > 4) || (val3 <= 0)) 
	{
	  printError(INVALID_VALUE);
	  return EXIT_FAILURE;
	}
      selectBalls(val2, val3);
      return EXIT_SUCCESS;
    }
  if (strcmp(argv[1], "-p") == 0)
    {
      if ((val2 < 1) || (val2 < val3))
	{
	  printError(INVALID_VALUE);
	  return EXIT_FAILURE;
	}
      partitionInteger(val2, val3);
      return EXIT_SUCCESS;
    }
  if (strcmp(argv[1], "-s") == 0)
    {
      if ((val2 < 1) || (val2 > 26))
	{
	  printError(INVALID_VALUE);
	  return EXIT_FAILURE;
	}
      findSubsets(val2, val3);
      return EXIT_SUCCESS;
    }  
  return EXIT_FAILURE;
}

void printError(int code)
{
  switch (code)
    {
    case INVALID_VALUE:
      printf("invalid value of the input\n");
      break;
    default:
      printf("unknown error code\n");
    }
}

void printBalls(int * cls, int numBalls)
{
  int iter;
  for (iter = 0; iter < numBalls; iter ++)
    {
      switch (cls[iter])
	{
	case RED: 
	  printf("R");
	  break;
	case BLUE: 
	  printf("B");
	  break;
	case GREEN:
	  printf("G");
	  break;
	case YELLOW:
	  printf("Y");
	  break;	  	  
	}
    }
  printf("\n");
}
void printPartition(int * usedInts, int ind)
{
  int iter;
  for (iter = 0; iter < ind; iter ++)
    {
      printf("%d ", usedInts[iter]);
    }
  printf("\n");
}

void printSubset(int * includeElement, int size, int maxSize)
{
  /* check whether the subset size is smaller than the allowed
     maxSize */
  int sum = 0;
  int iter;
  for (iter = 0; iter < size; iter ++)
    {
      sum += includeElement[iter];
    }
  if (sum > maxSize) 
    {
      return; /* do not print */
    }
  for (iter = 0; iter < size; iter ++)
    {
      if (includeElement[iter] == 1)
	{
	  printf("%c ", 'A' + iter);
	}
    }
  printf("\n");
}
